




using System;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.SaleOrder
{
    public class SaleOrderEntities : ERPModuleEntities
    {
        #region Declare Constant
        public readonly static String strARSaleOrdersObjectName = "ARSaleOrders";

        public readonly static String strARSaleOrderItemsObjectName = "ARSaleOrderItems";
        #endregion
        
        #region Declare all entities variables
        private ARSaleOrdersInfo _aRSaleOrdersObject;
        private ARSaleOrderItemsInfo _aRSaleOrderItemsObject;
 
        public THSItemsEntityList<SaleOrderItemsEntity> lstARSaleOrderItems;
        #endregion

        #region Public Properties
        public ARSaleOrdersInfo ARSaleOrdersObject
        {
            get
            {
                return _aRSaleOrdersObject;
            }
            set
            {
                _aRSaleOrdersObject = value;
            }
        }
        public ARSaleOrderItemsInfo aRSaleOrderItemsObject
        {
            get
            {
                return _aRSaleOrderItemsObject;
            }
            set
            {
                _aRSaleOrderItemsObject = value;
            }
        }

        public THSItemsEntityList<SaleOrderItemsEntity> ARSaleOrderItemsList
        {
            get
            {
                return lstARSaleOrderItems;
            }
            set
            {
                lstARSaleOrderItems = value;
            }
        }      
       
        #endregion

        #region Constructor
        public SaleOrderEntities()
            : base()
        {
            _aRSaleOrdersObject = new ARSaleOrdersInfo();

            _aRSaleOrderItemsObject = new ARSaleOrderItemsInfo();

            lstARSaleOrderItems = new THSItemsEntityList<SaleOrderItemsEntity>();
        }

        #endregion

        #region Init Main Object,Module Objects functions
        public override void InitMainObject()
        {

            MainObject = ARSaleOrdersObject;
        }

        public override void InitModuleObjects()
        {
            ModuleObjects.Add("ARSaleOrderItems", aRSaleOrderItemsObject);
        }

        public override void InitModuleObjectList()
        {            
            lstARSaleOrderItems = new THSItemsEntityList<SaleOrderItemsEntity>();
            //lstARSaleOrderItems.InitTHSList(this, THSUtil.GetTableNameFromBusinessObjectType(typeof(ARSaleOrderItemsInfo)));
            lstARSaleOrderItems.InitTHSList(this, "ARSaleOrders", "ARSaleOrderItems");
        }

        public override void InitGridControlInTHSList()
        {
            lstARSaleOrderItems.InitTHSListGridControl();
        }

        public override void SetDefaultModuleObjectsList()
        {
            try
            {
                lstARSaleOrderItems.SetDefaultListAndRefreshGridControl();

            }
            catch (Exception)
            {
                return;
            }
        }

        #endregion

        #region Invalidate Module Objects functions


        public override void InvalidateModuleObjects(int iObjectID)
        {
            ARSaleOrderItemsList.Invalidate(iObjectID);
        }


        #endregion

        #region Save Module Objects functions

        public override void SaveModuleObjects()
        {
            if(Module!=null)
            {
                if (Module.Toolbar.IsEditAction())
                {
                    THSItemsEntityList<SaleOrderItemsEntity> lstOldSaleOrderItemsEntity = new THSItemsEntityList<SaleOrderItemsEntity>();
                    //lstOldSaleOrderItemsEntity.InitTHSList(this, THSUtil.GetTableNameFromBusinessObjectType(typeof(ARSaleOrderItemsInfo)));
                    lstOldSaleOrderItemsEntity.InitTHSList(this, 
                                                            THSUtil.GetTableNameFromBusinessObjectType(typeof(ARSaleOrdersInfo)), 
                                                            THSUtil.GetTableNameFromBusinessObjectType(typeof(ARSaleOrderItemsInfo)));
                    lstOldSaleOrderItemsEntity.InvalidateAndNotUpdateModuleObjects(Module.Toolbar.CurrentObjectID);
                    lstOldSaleOrderItemsEntity.UpdateInventory(BusinessObject.DeletedAAStatus);
                }
            }
            

            ARSaleOrderItemsList.SaveItemObjects(true);

            lstARSaleOrderItems.UpdateInventory(BusinessObject.DefaultAAStatus);
        }

        #endregion

        public override void SetValuesAfterValidateProduct(int iICProductID)
        {
            //base.SetDefaultValueFromProduct(iICProductID);
            ARSaleOrderItemsList.SetDefaultValuesFromProduct(iICProductID);
        }

        public override void CalculateTotalInformation(ref double dbTotalPrice, ref double dbTotalItemDiscountAmount, ref double dbDiscountAmount, ref double dbTotalTaxAmount, ref double dbTotalBuyingPrice, ref double dbTotalItemNetAmount, ref double dbTotalNetAmount, ref double dbTotalGrossAmount, ref double dbMarginPercent)
        {                       

            foreach (SaleOrderItemsEntity entSaleOrdersItem in ARSaleOrderItemsList)
            {
                ARSaleOrderItemsInfo objARSaleOrderItemsInfo = (ARSaleOrderItemsInfo)entSaleOrdersItem.SetToBusinessObject(strARSaleOrderItemsObjectName);
                //Calculate item total price
                dbTotalPrice += THSUtil.Round(objARSaleOrderItemsInfo.ARSaleOrderItemPrice,THSApp.PriceDecimal);

                //Calculate total item discount amount
                dbTotalItemDiscountAmount += THSUtil.Round(objARSaleOrderItemsInfo.ARSaleOrderItemPrice * objARSaleOrderItemsInfo.ARSaleOrderItemProductDiscount / 100,THSApp.PriceDecimal);

                //Calculate total item tax amount
                dbTotalTaxAmount += THSUtil.Round(objARSaleOrderItemsInfo.ARSaleOrderItemPrice * objARSaleOrderItemsInfo.ARSaleOrderItemProductTaxPercent / 100,THSApp.PriceDecimal);

                //Calculate buying price
                int iICProductID = objARSaleOrderItemsInfo.FK_ICProductID;
                ICProductsInfo objICProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(iICProductID);
                if (objICProductsInfo != null)
                {
                    dbTotalBuyingPrice += objICProductsInfo.ICProductSupplierPrice * entSaleOrdersItem.ARSaleOrderItemProductQty;
                }
            }

            //Calculate dbTotalItemNetAmount(ZSum)
            dbTotalItemNetAmount = dbTotalPrice - dbTotalItemDiscountAmount;

            //Calculate dbSellOrderDiscountAmount
            ARSaleOrdersInfo objARSaleOrdersInfo = (ARSaleOrdersInfo)MainObject;
            if (objARSaleOrdersInfo.ARSaleOrderIsDiscountPercentCheck)
                dbDiscountAmount = dbTotalItemNetAmount * objARSaleOrdersInfo.ARSaleOrderDiscountPerCent / 100;
            else
                dbDiscountAmount = objARSaleOrdersInfo.ARSaleOrderDiscountFix;

            //Calculate dbTotalNetAmount
            dbTotalNetAmount = dbTotalItemNetAmount - dbDiscountAmount;

            //Calculate Total Gross Amount
            dbTotalGrossAmount = dbTotalNetAmount + dbTotalTaxAmount;

            //Calculate Profit percent
            if (dbTotalPrice > 0)
                dbMarginPercent = (dbTotalItemNetAmount - dbTotalBuyingPrice) * 100 / dbTotalItemNetAmount;

        }

        public override void UpdateInventory(ERPModuleItemsEntity entItems, string strUpdateStatus)
        {
            

            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(MainObject);
            String strItemTableName = strMainTableName.Substring(0, strMainTableName.Length - 1) + "Items";
            BusinessObject objItemsInfo = ((ERPModuleItemsEntity)entItems).SetToBusinessObject(strItemTableName);
            String strItemProductTypeColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductType";
            String strItemProductType = THSDbUtil.GetPropertyValue(objItemsInfo, strItemProductTypeColumnName).ToString();

            //not update inventory if product is Service or Comment

            if (!strItemProductType.Contains(Product.ProductModule.cstProductTypeService) && !strItemProductType.Contains(Product.ProductModule.cstProductTypeComment))
            {
                int iICProductID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItemsInfo, "FK_ICProductID"));
                int iICStockID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItemsInfo, "FK_ICStockID"));
                int iICStockSlotID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objItemsInfo, "FK_ICStockSlotID"));

                String strItemProductQtyColumnName = strItemTableName.Substring(0, strItemTableName.Length - 1) + "ProductQty";

                double dbItemProductQty = Convert.ToDouble(THSDbUtil.GetPropertyValue(objItemsInfo, strItemProductQtyColumnName));

                String strItemStatus = THSDbUtil.GetPropertyValue(objItemsInfo, "AAStatus").ToString();

                if (strUpdateStatus == BusinessObject.DefaultAAStatus)
                {
                    //Update Inventory Stock
                    if (strItemStatus == BusinessObject.DefaultAAStatus)
                    {
                        int iICInventoryStockID = TransactionUtil.UpdateInventoryStock(
                                                    iICProductID, iICStockID,
                                                    dbItemProductQty,
                                                    TransactionUtil.cstInventorySaleOrder);

                        //Update Inventory Stock Slot
                        if (iICInventoryStockID > 0)
                        {
                            int iICInventoryStockSlotID = TransactionUtil.UpdateInventoryStockSlot(
                                                            iICInventoryStockID, iICProductID,
                                                            iICStockSlotID,
                                                            dbItemProductQty,
                                                            TransactionUtil.cstInventorySaleOrder);
                        }
                    }
                }
                else if (strUpdateStatus == BusinessObject.DeletedAAStatus)
                {
                    //Update Inventory Stock
                    int iICInventoryStockID = TransactionUtil.UpdateInventoryStock(
                                                iICProductID, iICStockID,
                                                -dbItemProductQty,
                                                TransactionUtil.cstInventorySaleOrder);

                    //Update Inventory Stock Slot
                    if (iICInventoryStockID > 0)
                    {
                        int iICInventoryStockSlotID = TransactionUtil.UpdateInventoryStockSlot(
                                                        iICInventoryStockID, iICProductID,
                                                        iICStockSlotID,
                                                        -dbItemProductQty,
                                                        TransactionUtil.cstInventorySaleOrder);
                    }
                }

            }            
        }
    }
}
